<template>
  <view class="pk-scroll" :style="{height: 'calc(100vh - '+height+')'}">
    <scroll-view :scroll-top="scrollTop" scroll-y="true" class="pk-scroll-y" :refresher-triggered="triggered" @scrolltoupper="upper" :refresher-threshold="100" @scrolltolower="lower" @scroll="scroll" :refresher-enabled="true" @refresherrefresh="refresherrefresh" @refresherrestore="onRestore" @refresherabort="onAbort">
			<slot />	
		</scroll-view>
    <view class="pk-scroll-loading" v-if="loading">
      <view class="pk-scroll-loadingicon"></view>
      <view class="pk-scroll-loadingtext mt-24">loading</view> 
    </view>
  </view>
</template>

<script setup>
import { defineProps, defineEmits, ref } from 'vue';
const emits = defineEmits(['upper', 'lower', 'scroll', 'refresh'])
const scrollTop = ref(0)
const triggered = ref(false)
let isRefreshing = false

defineProps({
  height: {
    type: String,
    default: 0
  },
  loading: {
    type: Boolean,
    default: false
  }
})
const upper = (e) => {
  emits('upper')
}
const lower = (e) => {
  emits('lower')
}
const scroll = () => {
  emits('scroll')
}

const refresherrefresh = () => {
  if (isRefreshing) return
  isRefreshing = true
  if (!triggered.value) {
    triggered.value = true
  }
  emits('refresh')  
  setTimeout(() => {
    triggered.value = false
    isRefreshing = false
  }, 300)
}

const onRestore = () => {
  console.log('-------------onRestore-----')
  triggered.value = 'restore'
}
</script>

<style lang="scss" scoped>
.pk-scroll{
  height: 100%;
  flex:1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  position: relative;
  &-y{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  &-loadingicon{
    background: url("@/static/loading.png") center center  no-repeat;
    background-size: 56rpx auto;
    width: 56rpx;
    height: 56rpx;
    animation: rotate 1s linear infinite;
  }
  &-loadingtext{
    color: #00b4b3;
  }
  &-loading{
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    text-align: center;
    padding: 10px;
    background: rgba($color: #fff, $alpha: 0.8);
    backdrop-filter: 2px;
    top:0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
}

@keyframes rotate {
    0%{-webkit-transform:rotate(0deg);}
    25%{-webkit-transform:rotate(90deg);}
    50%{-webkit-transform:rotate(180deg);}
    75%{-webkit-transform:rotate(270deg);}
    100%{-webkit-transform:rotate(360deg);}
}

</style>